using System;
using System.IO;
using dbvc.Context;
using dbvc.context.Project;
using dbvc.context.Script;
using dbvc.context.Version;
using dbvc.data;
using dbvc.Queries;
using NUnit.Framework;

namespace tdd.dbvc
{
    public partial class tests
    {
        [Test]
        public void create_package()
        {
            security.Login("root", "root");

            var projTest = new CreateProject(security.Principal, new db_project() { name = "TEST", active = true }).Execute();

            var v1 = new CreateVersion(security.Principal, projTest, new db_version() { name = "0.1", on_date = DateTime.Today.AddDays(1) }).Execute();
            var v2 = new CreateVersion(security.Principal, projTest, new db_version() { name = "0.4", on_date = DateTime.Today.AddDays(4) }).Execute();
            var v4 = new CreateVersion(security.Principal, projTest, new db_version() { name = "0.3", on_date = DateTime.Today.AddDays(3) }).Execute();
            var v3 = new CreateVersion(security.Principal, projTest, new db_version() { name = "0.2", on_date = DateTime.Today.AddDays(2) }).Execute();

            var s1 = new CreateScript(security.Principal, new db_script() { name = "X1", active = true, text = "sql" }).Execute();
            var s2 = new CreateScript(security.Principal, new db_script() { name = "X2", active = true, text = "sql" }).Execute();
            var s3 = new CreateScript(security.Principal, new db_script() { name = "X3", active = true, text = "sql" }).Execute();
            var s4 = new CreateScript(security.Principal, new db_script() { name = "X4", active = true, text = "sql" }).Execute();
            var s5 = new CreateScript(security.Principal, new db_script() { name = "X5", active = true, text = "sql" }).Execute();
            var s6 = new CreateScript(security.Principal, new db_script() { name = "X6", active = true, text = "sql" }).Execute();
            var s7 = new CreateScript(security.Principal, new db_script() { name = "X7", active = true, text = "sql" }).Execute();
            var s8 = new CreateScript(security.Principal, new db_script() { name = "X8", active = true, text = "sql" }).Execute();
            var s9 = new CreateScript(security.Principal, new db_script() { name = "X9", active = true, text = "sql" }).Execute();
            var s10= new CreateScript(security.Principal, new db_script() { name = "X10",active = true, text = "sql" }).Execute();
            var s11= new CreateScript(security.Principal, new db_script() { name = "X11",active = true, text = "sql" }).Execute();
            var s12= new CreateScript(security.Principal, new db_script() { name = "X12",active = true, text = "sql" }).Execute();

            new AttachScript(security.Principal, v1, new[]{ s12,s1, s7 }).Execute();
            new AttachScript(security.Principal, v2, new[]{ s2, s4, s3 }).Execute();
            new AttachScript(security.Principal, v3, new[]{ s11,s5, s6 }).Execute();
            new AttachScript(security.Principal, v4, new[]{ s8, s9,s10 }).Execute();

            if (File.Exists(@"C:\Temp\result.xml")) File.Delete(@"C:\Temp\result.xml");

            new CreatePackage(projTest.id, security.Principal, new[]{v2.id, v4.id, v1.id, v3.id}, @"C:\Temp\result.xml").Execute();

            Assert.AreEqual(true, File.Exists(@"C:\Temp\result.xml"));
        }
    }
}